/* -*- C++ -*- */

/****************************************************************************
** Copyright (c) quickfixengine.org  All rights reserved.
**
** This file is part of the QuickFIX FIX Engine
**
** This file may be distributed under the terms of the quickfixengine.org
** license as defined by quickfixengine.org and appearing in the file
** LICENSE included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.quickfixengine.org/LICENSE for licensing information.
**
** Contact ask@quickfixengine.org if any conditions of this licensing are
** not clear to you.
**
****************************************************************************/

#ifndef FAST_PARSER_H
#define FAST_PARSER_H

#ifdef _MSC_VER
#pragma warning( disable : 4503 4355 4786 4290 )
#endif

#include "Exceptions.h"
#include <iostream>
#include <sstream>
#include <string>

#include "Parser.h"
#include "common.h"
#include "FASTApi.h"
#include "deque"

namespace FIX {

class SocketThreadData;


//Group = Len (u32) + PMap + TemplateID (u32)
	/// Parses %FAST messages off an input stream.
	class FASTParser: public Parser {
	public:
		FASTParser(std::string socketLogName) : Parser(socketLogName) {
			m_threadid = NULL;
			pSocketThreadData = NULL;
			m_Run = true;
			m_msgCount = 0;
		}

		~FASTParser() {
			m_Run = false;
			for(int i=0; i < m_totalSocketThreadCount; i++)
			{
				FIX::thread_join(*m_threadid[i]);
				delete pSocketThreadData[i];
				delete m_threadid[i];
			}
			if (pSocketThreadData)
				delete [] pSocketThreadData;
			if (m_threadid)
				delete [] m_threadid;
			pSocketThreadData = NULL;
			m_threadid        = NULL;
		}

		void reset(unsigned int tid = 0);

		bool extractLength(int& length, std::string::size_type& pos,
			const std::string& buffer) throw (MessageParseError);

		virtual bool readMessage(std::string& str) throw (MessageParseError);

		virtual void readFromStream(int socket) throw (SocketRecvFailed);

		virtual void writeMessage(const std::string& str) throw (MessageParseError);

		virtual bool writeToStream(int socket) throw (SocketRecvFailed);

		virtual void readFromStream( int  socket,  sockaddr_in &addr) throw ( SocketRecvFailed );
		virtual bool writeToStream( int  socket, const sockaddr_in &addr) throw ( SocketRecvFailed );

		Queue m_iq;
		Queue m_parser_iq;
		Mutex m_iq_mutex;
		unsigned m_msgCount;

		FASTApi iFast, oFast;
		unsigned int** m_threadid;
		SocketThreadData** pSocketThreadData;
		bool m_Run;

	private:

		void decodeTags(int tid, std::stringstream & ss, std::string msg_type = "");
		void encodeTags(int tid, std::map<int, std::string> tagvalues);
		void encodeTags(int tid, const FIX::Message &msg, int groupCount = -1);

	};
	class SocketThreadData
{
	public:
		SocketThreadData(FIX::FASTParser &parser, 
			int socket, 
			sockaddr_in addr)
			:m_parser(parser),
			m_socket(socket),
			m_addr(addr)
		{
			
		}
		~SocketThreadData(){}

		FIX::FASTParser &m_parser;
		int m_socket;
		sockaddr_in m_addr;
		FASTParser::Queue m_local_iq;
		char m_sockbuffer[1024];
	};
}

#endif //FAST_PARSER_H
